package com.lastproject.ejb.dao;

import java.util.List;

import com.lastproject.dto.UserDTO;
import com.lastproject.ejb.entities.Role;
import com.lastproject.ejb.interceptors.PermissionInterceptor;

import javax.ejb.Stateless;
import javax.faces.context.FacesContext;
import javax.interceptor.Interceptors;
import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.PersistenceContext;

import annotations.PermissionAnnotation;

/**
 * Session Bean implementation class MyBean
 */
@Stateless
public class CheckPermissionDAO implements CheckPermissionDAORemote {

	/**
	 * Default constructor.
	 */

	@PersistenceContext
	EntityManager entityManager;

	public CheckPermissionDAO() {

	}

	@Interceptors(PermissionInterceptor.class)
	@PermissionAnnotation(canOpenGeneralReports = 1)
	@Override
	public boolean hasOpenGeneralReportsRights(Boolean hasPermission) {

		return hasPermission;
	}

	@Interceptors(PermissionInterceptor.class)
	@PermissionAnnotation(canCreateGeneralReports = 1)
	@Override
	public boolean hasCreateGeneralReportsRights(Boolean hasPermission) {

		return hasPermission;
	}

	@Interceptors(PermissionInterceptor.class)
	@PermissionAnnotation(canOpenConfidentialReports = 1)
	@Override
	public boolean hasOpenConfidentialReportsRights(Boolean hasPermission) {

		return hasPermission;
	}

	@Interceptors(PermissionInterceptor.class)
	@PermissionAnnotation(canCreateConfidentialReports = 1)
	@Override
	public boolean hasCreateConfidentialReportsRights(Boolean hasPermission) {

		return hasPermission;
	}

	@Interceptors(PermissionInterceptor.class)
	@PermissionAnnotation(canCRUDOnEmployeesConfidential = 1)
	@Override
	public boolean hasEmployeeManagementRights(Boolean hasPermission) {

		return hasPermission;
	}

	@Interceptors(PermissionInterceptor.class)
	@PermissionAnnotation(canCRUDOnEmployees = 1)
	@Override
	public boolean hasCrudOnEmployeesRights(Boolean hasPermission) {

		return hasPermission;
	}

	@Interceptors(PermissionInterceptor.class)
	@PermissionAnnotation(canCRUDOnCandidate = 1)
	@Override
	public boolean hasCandidateManagementRights(Boolean hasPermission) {

		return hasPermission;
	}

	@Interceptors(PermissionInterceptor.class)
	@PermissionAnnotation(canCRUDOnEmployeesByDepartment = 1)
	@Override
	public boolean hasDepartmentManagementRights(Boolean hasPermission) {

		return hasPermission;
	}

	@Interceptors(PermissionInterceptor.class)
	@PermissionAnnotation(canCRUDOnEmployeesByTeam = 1)
	@Override
	public boolean hasTeamManagementRights(Boolean hasPermission) {

		return hasPermission;
	}

	@Interceptors(PermissionInterceptor.class)
	@PermissionAnnotation(canCRUDOnEmployeesConfidential = 1)
	@Override
	public boolean hasAccountingRights(Boolean hasPermission) {

		return hasPermission;
	}

	@Interceptors(PermissionInterceptor.class)
	@PermissionAnnotation(canAccessLibrary = 1)
	@Override
	public boolean hasLibraryManagementRights(Boolean hasPermission) {
		return hasPermission;
	}

	@Interceptors(PermissionInterceptor.class)
	@PermissionAnnotation(canCRUDOnBooks = 1)
	@Override
	public boolean hasLibraryRights(Boolean hasPermission) {
		return hasPermission;
	}

	@Interceptors(PermissionInterceptor.class)
	@PermissionAnnotation(canCreateQuiz = 1)
	@Override
	public boolean hasSurveyManagementRights(Boolean hasPermission) {

		return hasPermission;
	}


	@Interceptors(PermissionInterceptor.class)
	@PermissionAnnotation(cannotViewSuggestion = 1)
	@Override
	public boolean cannotViewSuggestion(Boolean hasPermission) {
		return hasPermission;
	}
	
	//@Interceptors(PermissionInterceptor.class)
	//@PermissionAnnotation(canViewEmployeeManagement = 1)
	@Override
	public boolean isLimitedUser(Boolean hasPermission) {
		//return hasPermission;
		UserDTO u = (UserDTO) FacesContext.getCurrentInstance()
				.getExternalContext().getSessionMap().get("userDTO");

		int id = u.getId();
		System.out.println("USER ID: "+id);
		try{
		List<Role> roleList = entityManager
				.createQuery(
						"SELECT r FROM Role r JOIN r.users u WHERE u.id=:id",Role.class)
				.setParameter("id", id).getResultList();
		System.out.println("ROLE LIST SIZE: "+roleList.size());
		for (Role r : roleList){
			if (r.getName().compareTo("limitedUser")==0 && roleList.size()==1) return true;
		}
		
		}
		catch(NoResultException e){
			e.printStackTrace();
			return false;
		}

		
		return false;
	}

	@Override
	public boolean isBasicUser() {

		UserDTO u = (UserDTO) FacesContext.getCurrentInstance()
				.getExternalContext().getSessionMap().get("userDTO");

		int id = u.getId();
		System.out.println("USER ID: "+id);
		try{
		List<Role> roleList = entityManager
				.createQuery(
						"SELECT r FROM Role r JOIN r.users u WHERE u.id=:id",Role.class)
				.setParameter("id", id).getResultList();
		System.out.println("ROLE LIST SIZE: "+roleList.size());
		for (Role r : roleList){
			if (r.getName().compareTo("basicUser")==0 && roleList.size()==1) return true;
		}
		
		}
		catch(NoResultException e){
			e.printStackTrace();
			return false;
		}

		
		return false;
	}
	
	@Override
	public boolean isLibraryAdmin(){
		UserDTO u = (UserDTO) FacesContext.getCurrentInstance()
				.getExternalContext().getSessionMap().get("userDTO");

		int id = u.getId();
		System.out.println("USER ID: "+id);
		try{
		List<Role> roleList = entityManager
				.createQuery(
						"SELECT r FROM Role r JOIN r.users u WHERE u.id=:id",Role.class)
				.setParameter("id", id).getResultList();
		System.out.println("ROLE LIST SIZE: "+roleList.size());
		for (Role r : roleList){
			if (r.getName().compareTo("libraryAdmin")==0  || r.getName().compareTo("superAdmin")==0) return true;
		}
		
		}
		catch(NoResultException e){
			e.printStackTrace();
			return false;
		}

		
		return false;
	}
	
	@Interceptors(PermissionInterceptor.class)
	@PermissionAnnotation(canViewEmployeeManagement = 1)
	@Override
	public boolean canViewEmployeeManagement(Boolean hasPermission) {
		return hasPermission;
	}
	
	@Interceptors(PermissionInterceptor.class)
	@PermissionAnnotation(canViewSurveyManagement = 1)
	@Override
	public boolean canViewSurveyManagement(Boolean hasPermission) {
		return hasPermission;
	}

}
